package cn.itcast.service.impl;

import cn.itcast.constant.RedisConstant;
import cn.itcast.dao.CheckGroupDao;
import cn.itcast.dao.CheckItemDao;
import cn.itcast.dao.SetmealDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.CheckItem;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.SetmealService;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mchange.v2.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @author: luoqianjin
 * @date: 2019/7/28 20:20
 * @description:体检套餐服务实现类
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;
    //新增套餐
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            //绑定套餐和检查组的多对多关系
            setSetmealAndCheckGroup(setmeal.getId(), checkgroupIds);
        }
        //将图片名称保存到Redis
        savePic2Redis(setmeal.getImg());
    }
    //将图片名称保存到Redis
    private void savePic2Redis(String pic) {
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, pic);
    }

    //绑定套餐和检查组的多对多关系
    private void setSetmealAndCheckGroup(Integer id, Integer[] checkgroupIds) {
        for (Integer checkgroupId : checkgroupIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("setmeal_id", id);
            map.put("checkgroup_id", checkgroupId);
            setmealDao.setSetmealAndCheckGroup(map);
        }
    }
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //在SetmealServiceImpl服务实现类中实现分页查询方法，基于Mybatis分页助手插件实现分页
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }


    @Override
    public List<Setmeal> getSetmeal() {
        return setmealDao.getSetmeal();
    }

    /**
     *  查询套餐详情(1.基本信息  2.检查组信息   3.每个检查组对应的检查项信息)
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(int id) {
        //查询基本信息
        Setmeal setmeal = setmealDao.findByIdBetter(id);
        /*if (!org.springframework.util.ObjectUtils.isEmpty(setmeal)) {
            //d对套餐对应的检查组进行遍历，获取每一个检查组信息
            List<CheckGroup> checkGroupList = checkGroupDao.findCheckgroupBySetmealId(id);
            if (!CollectionUtils.isEmpty(checkGroupList)) {
                checkGroupList.forEach(checkGroup->{
                    //对检查组进行遍历，获取每个检查项信息
                  List<CheckItem> checkItemList =  checkItemDao.findCheckItemByCheckgroupId(checkGroup.getId());
                  checkGroup.setCheckItems(checkItemList);
                });
            }
            setmeal.setCheckGroups((checkGroupList));
        }*/
        return setmeal;
    }

    @Override
    public List<Map<String, String>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }


}
